class GameUI extends UIBase{
    private fruit_group:eui.Group;
    private game_group:eui.Group;
    private btn_share:BaseButton;
    private btn_three:BaseButton;
    private btn_change:BaseButton;
    private lb_customs:eui.BitmapLabel;
    private img_shadow:eui.Image;
    private rect_black:eui.Image;
    private mid_group:eui.Group;

    private isIncheck:boolean;
    private count:number=0;
    private testPoint:egret.Point;

    private knife:knife;
    private knife1:knife;
    private knife2:knife;

    private three_group:eui.Group;
    private lb_times:eui.Label;
    private lb_di:eui.BitmapLabel;
    private lb_guan:eui.BitmapLabel;

    //当前关卡所值得金币数
    // private curCustomsMoney:number;

    //当前关卡击空的次数
    private curHitlost:number;

    //一次飞刀击中的水果数
    private knifeHitNum:number;
    private dragon_group:eui.Group;
    private lb_dragon_times:eui.Label;

    private dragonEffect:DragonEffectUI;
    private img_bg:eui.Image;
    private img_light:eui.Image;
    private img_float_world:eui.Image;
    private img_miss:eui.Image;
    private totalNum:number;
    private btn_home:BaseButton;

    private system:particle.ParticleSystem;
    private system1:particle.ParticleSystem;
    private system2:particle.ParticleSystem;

    private lb_curCustoms:eui.Label;
    private img_progress:eui.Image;

    private group_star:eui.Group;

    private leaf:particle.ParticleSystem;
    private img_spt9:eui.Image;
    private gif_box:egret.MovieClip;

    //水果集合
    private fruit_list = [];

    private isPlayBoxAnimation:boolean = false;
    private speedX = 1;
    private speedY = 1;
    private isBackX = false;
    private isBackY = false;

    private isKnifeInBack = false;//刀是否正在处于回旋状态
    private BackStayTime = 0;
    private lb_tips:eui.Label;//小提示
    private group_tips:eui.Group;//提示多少关获得武器
    private lb_need_customs:eui.Label;
    
    private cut_fruitList = {};//本局游戏中切的水果各个的个数

    constructor(data?)
    {
        super('GameView',data);
    }

    /**
     * 初始化
     */
    public init()
    {
        super.init(true);
        this.reviewHide();
        EffectUtil.instance().StopEffect(this.fruit_group);
        EffectUtil.instance().scaleEffect(this.btn_share);
        this.fruit_group.rotation = 0;
        this.fruit_group.removeChildren();
        this.isIncheck = false;
        this.curHitlost = 0;
        this.totalNum = 0;
        if(!this.testPoint)
        this.testPoint = new egret.Point();
        /**
         * init some compsite status
         */
        this.showParticle();
        this.addLeaf();
        // this.setDate();
        this.initStar();
        this.initGif();
        this.randomSetTips();

        /**
         * check current customs 
         */
        GameManager.instance.checkIsHaveAward(true);
        if(GameManager.instance.isCanshowBox)
        {
            GameManager.instance.isCanshowBox = false;
            this.playBoxAnimation();
        }

        /**
         * init ui
         */
        this.updateThreeGroup(true);
        this.updateDragonGroup(true);
        this.initFruitUI();
        this.initPosAndAnimation();

        this.UpdateTittle();
        this.updateCurCustoms();
        this.initDragonTimes();
        this.updateNextCustoms();
        if(GameManager.instance.curCustoms < 8)
        {
            EffectUtil.instance().AlphaEffect(this.group_tips);
        }
    }

    /**
     * gif初始化
     */
    public initGif()
    {
        if(!this.gif_box)
        {
            this.gif_box = MovieManager.instance().getGifByName('fly_box','fly_box');
            this.mid_group.addChild(this.gif_box);
            this.gif_box.visible = false;
            this.gif_box.scaleX = this.gif_box.scaleY = 0.3;
        }
    }

    /**
     * 展示宝箱动画
     */
   public playBoxAnimation()
   {
       this.gif_box.x = 60;
       this.gif_box.y = 100;
       this.gif_box.visible = true;
       this.isBackX = false;
       this.isBackY = false;
       this.gif_box.play(-1);
       this.isPlayBoxAnimation = true;
   }

   /**
    * 停止宝箱动画
    */
   public stopBoxAnimation()
   {
       this.gif_box.visible = false;
       this.gif_box.stop();
       this.isPlayBoxAnimation = false;
   }

   /**
    * 设置背景
    */
   public setDate()
    {
        if(TimeUtil.getIsNight())
        {
            this.img_bg.source = 'bg2_png';
            this.img_spt9.source = 'spt9_bg_di_2_png';
        }
        else
        {
            this.img_bg.source = 'bg_jpg';
            this.img_spt9.source = 'spt9_bg_di_png';
        }
    }

    /**
     * 设置随机小提示
     */
    public randomSetTips()
    {
        let tips_str:string = DataJson.const_define.Fruit_encyclopedia.award;
        let arr = tips_str.split('|');
        let index = Math.floor(Math.random() * arr.length);
        let str = arr[index];
        this.lb_tips.text = '小提示：'+str+'呦~';  
    }

    /**
     * 飘的叶子
     */
    protected addLeaf()
    {
        let texture = RES.getRes('leaf_png');
        let config = RES.getRes("leaf_json");
        this.leaf = new particle.GravityParticleSystem(texture, config);
        this.addChildAt(this.leaf,1);
        this.leaf.emitterX = 0;
        this.leaf.emitterY = 0;
        this.leaf.start();
    }

    /**
     * 星星的初始化或者每次变更初始化
     */
    public initStar()
    {
        // this.group_star.removeChildren();
        let starNum = GameManager.instance.getCurCustomsHitLostNum();
        let curNum = this.group_star.numChildren;
        if(curNum > starNum)
        {
            for(let i = curNum-1;i>starNum-1;i--)
            {
                let img = this.group_star.getChildByName('star_'+i);
                if(img)
                {
                    this.group_star.removeChild(img);
                }
            }
        }
        else
        {
            for(let i=curNum-1;i<starNum;i++)
            {
                let img = new eui.Image;
                this.group_star.addChild(img);
            }
        }
        for(let i=0;i<starNum;i++)
        {
            let img = this.group_star.getChildAt(i) as eui.Image;
            if(img)
            {
                img.name = 'star_' + i;
                img.source = 'cutter3_07';
            }
        }
    }

    /**
     * 审核状态
     */
    public reviewHide()
    {
        if(RoleCache.instance().reviewStatus)
        {
            this.btn_three.visible = false;
            this.btn_share.visible = false;
        }
    }

    /**
     * 星星的变化
     */
    public updateStar()
    {
        let index = this.curHitlost - 1;
        if(index >= 0 && this.group_star.numChildren > 0)
        {
            let img = this.group_star.getChildByName('star_'+index) as eui.Image;
            if(img)
            img.source = 'cutter3_09';
        }
    }

    /**
     * 更新当前的关卡
     */
    public updateCurCustoms()
    {
        let customsIndex = GameManager.instance.curCustomsIndex;
        let progress = customsIndex / 4;
        progress = progress > 1 ? 1 : progress;
        let barMask = new egret.Rectangle(0, 0, progress * this.img_progress.width, this.img_progress.height);
        // this.img_progress.width = progress * 121;
        this.img_progress.mask = barMask;
        this.lb_curCustoms.text = customsIndex + ' / 4';
        // this.initStar();
    }

    public addEvent()
    {
        this.game_group.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touchHandle, this);
        egret.startTick(this.update, this);

        this.btn_share.addTapHandler(this.KillDragonHandle, this);
        this.btn_change.addTapHandler(this.changeWepon,this);
        this.btn_three.addTapHandler(this.ThreeHandle,this);
        this.btn_home.addTapHandler(this.BackHome, this);

        GameUtil.addEventListener(EventConst.SHARE_SUCCESS, this.shareSuccess, this);
        GameUtil.addEventListener(EventConst.CHANGE_WEAPON, this.changeWeapon, this);
    }

    /**
     * 切换武器后的处理
     */
    protected changeWeapon()
    {
        this.showParticle();
        this.initStar();
        this.updateStar();
    }

    /**
     * 返回主页
     */
    public BackHome()
    {
        let self = this;
        let sureCall = function()
        {
            ScreenMovies.MovieStart(2,()=>{
            let da = {
                index:0
            }
            UIManager.instance().openUILogic(MainUI, da);
            },self);
        }
        let data = {
            tips:'确定回到主页?',
            sureCall:sureCall,
            isPop:true,
            effect:1,
            scope:this
        }
        GameUtil.openConfirmUI(data)
    }

    /**
     * 游戏中分享处理
     */
    protected shareSuccess(e:BaseEvent)
    {
        let type = e.param['type'];
        if(type == ShareId.ThreeKnife)
        {
            // GameManager.instance.curThreeTimes += GameConst.GameConst.three_count;
            ResourceUtil.instance().addResource('a_4_'+GameConst.GameConst.three_count);
            UIManager.instance().closeUILogic(UIConst.ALERT_UI);
            this.initPosAndAnimation();
            // this.updateThreeGroup(true);
            GameManager.instance.addTodayThreeShareTimes();
            egret.setTimeout(()=>{
                MessageUtil.tips('领取成功！');
             }, this, 300);
        }
        else if(type == ShareId.killDragon)
        {
            // GameManager.instance.curDragonTimes += GameConst.GameConst.kill_dragon_count;
            ResourceUtil.instance().addResource('a_3_'+GameConst.GameConst.kill_dragon_count);
            GameManager.instance.addTodayDragonShareTime();
            UIManager.instance().closeUILogic(UIConst.ALERT_UI);
            egret.setTimeout(()=>{
                MessageUtil.tips('领取成功！');
            }, this, 300);
            // this.initDragonTimes();
            // this.updateDragonGroup(true);
        }
    }

    /**
     * 展示游戏中的武器的粒子特效
     */
    public showParticle()
    {
        let data = this.knife.da;
        let effect = '';
        let curWeaponId = RoleCache.instance().curWeaponId;
        data = data? data : WeaponTpl.instance().getWeaponDataByWeaponId(curWeaponId);
        if(data.effect != '')
        {
            let texture = RES.getRes(data.effect);
            let config = RES.getRes("particle_52_json");
            // let config1 = JSON.parse(JSON.stringify(config));
            // let config2 = JSON.parse(JSON.stringify(config));
            this.system = new particle.GravityParticleSystem(texture, config);
            this.mid_group.addChildAt(this.system,0);
            this.system.emitterX = this.knife.x;
            this.system.emitterY = this.knife.y + 20;
            
            this.system1 = new particle.GravityParticleSystem(texture, config);
            this.mid_group.addChildAt(this.system1, 0);
            this.system1.emitterX = this.knife1.x;
            this.system1.emitterY = this.knife1.y + 20;

            this.system2 = new particle.GravityParticleSystem(texture, config);
            this.mid_group.addChildAt(this.system2, 0);
            this.system2.emitterX = this.knife2.x;
            this.system2.emitterY = this.knife2.y + 20;
        }
        else
        {
            if(this.system)
            this.mid_group.removeChild(this.system);
            if(this.system1)
            this.mid_group.removeChild(this.system1);
            if(this.system2)
            this.mid_group.removeChild(this.system2);
            
            this.system = null;
            this.system1 = null;
            this.system2 = null;
        }
    }

    /**
     * 飘字
     */
    protected floatWorld()
    {
        let count = this.knifeHitNum;
        if(count == 1)return;
        if(count == 0)
        {
            this.img_miss.alpha = 0;
            this.img_miss.visible = true;
            egret.Tween.get(this.img_miss).to({
                y:this.img_miss.y - 50,
                alpha : 1
            }, 500).set({
                visible : false,
                y:this.img_miss.y
            })
        }
        else
        {
            let frame = 100/3;
            let source = '';
            if(count == this.totalNum)
            {
                source = 'AMAZING';
                SoundManager.instance().playSound('amazing');
                GameManager.instance.one_cut_num++;
            }
            else if(count < 6 && count > 1)
            {
                source = 'GOOD';
                SoundManager.instance().playSound('good');
            }
            else if(count == 6){
                source = 'GREAT';
                SoundManager.instance().playSound('great');
            }
            else if(count > 6){
                source = 'EXCELLENT';
                SoundManager.instance().playSound('excellent');
            }
            EffectUtil.instance().StopEffect(this.img_float_world);
            this.img_float_world.scaleX = this.img_float_world.scaleY = 0;
            this.img_light.scaleX = this.img_light.scaleY = 0;
            this.img_float_world.source = source;
            this.img_float_world.anchorOffsetX = this.img_float_world.width >> 1;
            this.img_float_world.anchorOffsetY = this.img_float_world.height >> 1;
            this.img_float_world.x = this.img_light.x;
            this.img_float_world.y = this.img_light.y;
            let self = this;
            let effect_1 = function()
            {
                egret.Tween.get(self.img_float_world).to({
                    scaleX:3,
                    scaleY:3
                }, 3 * frame).to({
                    scaleX:2,
                    scaleY:2
                }, 3 * frame).wait(24 * frame).set({
                    scaleX:0,
                    scaleY:0
                })

            }
            let effect_2 = function()
            {
                self.img_float_world.scaleX = self.img_float_world.scaleY = 2;
                // self.img_light.scaleX = self.img_light.scaleY = 1.1;
                let lastY = self.img_float_world.y;
                // let lastY1 = self.img_light.y;
                self.img_float_world.y = -400;
                // self.img_light.y = -400;
                egret.Tween.get(self.img_float_world).to({
                    y:lastY
                }, 6 * frame, egret.Ease.backOut).to({
                    scaleX:0,
                    scaleY:0
                }, 24 * frame, egret.Ease.backIn)
            }
            let random = Math.random();
            random > 0.5 ? effect_1() : effect_2();
        }
    }

    // //水果扩散的动画 （废弃）
    // public spreadAnimation()
    // {
    //     let fruit_list = this.fruit_list;
    //     let center_x = this.fruit_group.anchorOffsetX;
    //     let center_y = this.fruit_group.anchorOffsetY;
    //     let centerPoint = new egret.Point(center_x, center_y);
    //     let fruitPoint = new egret.Point();

    //     for(let fruit of fruit_list)
    //     {
    //         fruitPoint.x = fruit.x;
    //         fruitPoint.y = fruit.y;
    //         let distance = egret.Point.distance(fruitPoint, centerPoint);
    //         if(distance > 10)
    //         {
    //             let radian = Math.atan2(fruit.y, fruit.x);
    //             fruit.x += Math.cos(radian);
    //             fruit.y += Math.sin(radian);
    //         }
    //     }
    // }

    public initDragonTimes()
    {
        if(GameManager.instance.curDragonTimes > 0)
        {
            this.dragon_group.visible = true;
            this.lb_dragon_times.text = 'x'+GameManager.instance.curDragonTimes+'';
        }
        else
        {
            this.dragon_group.visible = false;
        }
    }

    protected UpdateTittle()
    {
        let curCustoms = GameManager.instance.curCustoms;
        
        this.lb_customs.text = curCustoms + '';
    }

    protected KillDragonHandle()
    {
        let kill_dragonTimes = GameManager.instance.curDragonTimes;
        if(kill_dragonTimes == 0)
        {
            let todayDragonShareTime = GameManager.instance.todayDragonShareTimes;
            if(todayDragonShareTime < GameConst.GameConst.dragon_limit_cnt)
            {
                let data = {
                    isPop:true,
                    type:alertType.killDragon
                }
                UIManager.instance().openUILogic(AlertUI, data);
            }
            else
            {
                MessageUtil.tips('今日可用的屠龙宝刀的数量已经到达了上限~');
            }
        }
        else
        {
            this.showDragonEffect();
        }
    }

    /**
     * 切换武器
     */
    protected changeWepon()
    {
        //换武器
        let data = {
            isPop : true,
            effect:0
        }
        UIManager.instance().openUILogic(WeaponNewSelectUI, data);
    }

    protected ThreeHandle()
    {
        let todayThreeShareTimes = GameManager.instance.todayThreeShareTimes;
        if(todayThreeShareTimes >= GameConst.GameConst.three_limit_cnt)
        {
            MessageUtil.tips('今日可用的三箭齐发已经达到上限!');
            return ;
        }
        if(GameManager.instance.curThreeTimes > 0){
            MessageUtil.tips('当前还有三发未使用完哦~')
        }
        else
        {
            //三发
            let data = {
                isPop:true,
                type:alertType.threeShare
            }
            UIManager.instance().openUILogic(AlertUI, data);
        }
    }

    protected touchHandle()
    {
        if(!this.isIncheck)
        {
            EffectUtil.instance().StopEffect(this.knife);
            SoundManager.instance().playCutLostSound();
            let times = GameManager.instance.curThreeTimes;
            if(times > 0)
            {
                EffectUtil.instance().StopEffect(this.knife1);
                EffectUtil.instance().StopEffect(this.knife2);
                this.knife1.y = this.knife2.y = GameConst.GameConst.knife_posY;
                this.particleStart();
            }
            if(this.system)
            {
                this.system.start();
            }
            this.isIncheck = true;
        }
    }

    protected particleStart()
    {
        if(!this.system1)return;
        this.system1.start();
        this.system2.start();
    }

    protected particleStop()
    {
        if(!this.system1)return;
        this.system1.stop();
        this.system2.stop();
    }

    public updateThreeGroup(isInit = false)
    {
        let threeTimes = GameManager.instance.curThreeTimes;
        if(threeTimes > 0)
        {
            if(!isInit)
            {
                GameManager.instance.reduceCurThreeTimes();
                threeTimes --;
            }
            if(threeTimes > 0)
            {
                this.three_group.visible = true;
                this.lb_times.text = 'x' + threeTimes;
            }
            else
            {
                this.three_group.visible = false;
            }
        }
        else
        {
            this.three_group.visible = false;
        }
    }

    protected update()
    {
        this.count++;
        let self = this;
        let update_knife_pos = function(knife:knife, ischeck:boolean = false)
        {
            knife.rotation += GameConst.GameConst.knife_rotation;
            //不回旋直接飞
            if(knife.y < -knife.height / 2 && !self.isKnifeInBack)
            {
                let property = knife.getProperty();
                if(property != WeaponPropertyType.comeBack)
                {
                   if(ischeck)
                    {
                        //检测一次
                        afterCheck();
                    }
                }
                else
                {   
                    self.isKnifeInBack = true;
                }
            }
            else
            {
                if(!self.isKnifeInBack)
                {
                    knife.y -= GameConst.GameConst.knife_speed;
                    if(ischeck)
                    {
                        if(self.system)
                        self.system.emitterY -= GameConst.GameConst.knife_speed;
                        self.particleMove();
                    }
                }
                else
                {
                    if(self.BackStayTime == 13)
                    {
                        knife.y += GameConst.GameConst.knife_speed;
                        if(ischeck)
                        {
                            if(self.system)
                            self.system.emitterY += GameConst.GameConst.knife_speed;
                            self.particleMove();
                        }
                        
                        if(knife.y >= GameConst.GameConst.knife_posY)
                        {
                            if(ischeck)
                            {
                                //检测一次
                                afterCheck();
                                self.isKnifeInBack = false;
                                self.BackStayTime = 0;
                            }
                        }
                    }
                    else
                    {
                        self.BackStayTime++;
                    }
                }
            }
        }

        let afterCheck = function()
        {
            self.floatWorld();
            self.updateThreeGroup();
            let isGameOver = self.checkIsGameOver();
            if(isGameOver)
            {
                self.gameOver();
            }
            else
            {
                let len = self.fruit_list.length;
                if(len <= 0)
                {
                    self.game_group.touchEnabled = false;
                    self.nextSmallCustoms();
                }
            }
            self.initPosAndAnimation();
        }

        if(this.isIncheck)
        {
            let times = GameManager.instance.curThreeTimes;
            if(times > 0)
            {
                update_knife_pos(this.knife1, false);
                update_knife_pos(this.knife2, false);
            }
            update_knife_pos(this.knife, true);
        }

        if(this.isPlayBoxAnimation)
        {
            let speedX = this.isBackX ? -1 * this.speedX: this.speedX;
            let speedY = this.isBackY ? -1 * this.speedY: this.speedY;
            this.gif_box.x += speedX;
            this.gif_box.y += speedY;
            if(this.gif_box.x > 500 && !this.isBackX)
            {
                this.isBackX = true;
            }
            if(this.gif_box.y > 130 && !this.isBackY)
            {
                this.isBackY = true;
            }
            else if(this.gif_box.y < 70 && this.isBackY)
            {
                this.isBackY = false;
            }
            if(this.gif_box.x < -100)
            {
                this.stopBoxAnimation();
            }
        }

        if(this.count % 2!= 0)
        return ;
        this.check();
        if(this.isPlayBoxAnimation)
        {
            let bool = this.checkHitBox();
            if(bool)
            {
                this.touchChildren = false;
                this.stopBoxAnimation();
                let data = {
                    isPop:true,
                    effect:1,
                    tips_id:'tips_3'
                }
                UIManager.instance().openUILogic(SmallConfirmUI,data);
                this.knife.scaleAnimation(()=>{
                    this.touchChildren = true;
                },this);
                let threeTimes = GameManager.instance.curThreeTimes;
                if(threeTimes > 0)
                {
                    if(this.knife1)
                    {
                        this.knife1.scaleAnimation(null,null);
                    }
                    if(this.knife2)
                    {
                        this.knife2.scaleAnimation(null,null);
                    }
                }
            }
        }
        return false;
    }

    //判断击空次数是否死亡
    private checkIsGameOver()
    {
        if(this.knifeHitNum == 0)
        this.curHitlost++;
        this.knifeHitNum = 0;
        let canHitLostNum = GameManager.instance.getCurCustomsHitLostNum();
        if(this.curHitlost > canHitLostNum)
        {
            return true;
        }
        this.updateStar();
        return false;
    }

    //更新下一个关卡
    public updateNextCustoms()
    {
        if(!this.group_tips.visible)return;
        let curCustoms = GameManager.instance.curCustoms;
        if(curCustoms >= 8)
        {
            this.group_tips.visible = false;
            EffectUtil.instance().StopEffect(this.group_tips);
        }
        else
        {
            let needCustoms = 8 - curCustoms;
            this.lb_need_customs.text = StringUtil.getSwfLangStrVar('tips_4', [needCustoms+'']);
        }
    }

    private check()
    {
        this.count = 0;
        let list = this.fruit_list;
        if(!list || !this.isIncheck)return;
        let len = this.fruit_list.length;
        
        let self = this;
        let times = GameManager.instance.curThreeTimes;
        let checkHit = function(bounds:egret.Rectangle, knife:knife)
        {   
            let dao = knife;
            let worldPoint = self.mid_group.localToGlobal(dao.x, dao.y, self.testPoint);
            let localPoint = self.fruit_group.globalToLocal(worldPoint.x, worldPoint.y,self.testPoint);
            let knife_bounds = dao.getBounds();
            if(dao.isScale)
            {
                //变大的刀范围扩大
                knife_bounds.width = knife_bounds.height = 170;
                knife_bounds.x = localPoint.x - 85;
                knife_bounds.y = localPoint.y - 85;
            }
            else
            {
                knife_bounds.width = knife_bounds.height = 120;
                knife_bounds.x = localPoint.x - 60;
                knife_bounds.y = localPoint.y - 60;
            }

            if(knife_bounds.intersects(bounds))
            {
                return true;
            }
            return false;
        }

        if(len > 0)
        {
            for(let i=0;i<len;i++)
            { 
                let fruit = list[i] as fruit;
                let bounds = fruit.getBounds();
                bounds.x = fruit.x - fruit.anchorOffsetX;
                bounds.y = fruit.y - fruit.anchorOffsetY;
                if(checkHit(bounds, this.knife) || (times > 0 && (checkHit(bounds, this.knife1) || checkHit(bounds, this.knife2))))
                {
                    this.knifeHitNum ++;
                    list.splice(i,1);
                    len--;
                    i--;
                    let fruitData = fruit.Data;
                    let data = {
                        id:fruitData.id,
                        color:fruitData.color
                    }

                    //添加爆炸效果
                    let item = fruitBoomItem.getBoomItem(data);
                    if(!this.game_group.contains(item))
                    this.game_group.addChildAt(item,1);
                    let w = this.fruit_group.localToGlobal(fruit.x, fruit.y,this.testPoint);
                    let l = this.game_group.globalToLocal(w.x, w.y, this.testPoint);
                    item.x = l.x;
                    item.y = l.y;
                    item.boom(fruit.boom, fruit);
                    WeChatgGameManager.instance().vibrateShort();
                }
            }
        }
    }

    public checkHitBox():boolean
    {
        //if three-knife times more than zero then return (aleady give up)
        // let times = GameManager.instance.curThreeTimes;
        // if(times > 0)return false;
        let times = GameManager.instance.curThreeTimes;
        let box = this.gif_box;
        let checkHit:Function = function(knife:knife)
        {
            let dao = knife;
            let knife_bounds = dao.getBounds();
            knife_bounds.width = knife_bounds.height = 120;
            knife_bounds.x = dao.x - 60;
            knife_bounds.y = dao.y - 60;
            let box_bounds = box.getBounds();
            box_bounds.x = box.x;
            box_bounds.y = box.y;
            box_bounds.width = 223.5;
            box_bounds.height = 84;
            return knife_bounds.intersects(box_bounds);
        }
        
        if(checkHit(this.knife) || (times > 0 && (checkHit(this.knife1) || checkHit(this.knife2))))
        {
            return true
        }
        return false;
    }

    public cutAllFruit()
    {
        let list = this.fruit_list;
        this.rect_black.visible = false;
        this.img_shadow.visible = false;
        this.dragonEffect.visible = false;
        EffectUtil.instance().StopEffect(this.fruit_group);
        this.img_shadow.alpha = 0;
        for(let i in list)
        {
            let fruit = list[i] as fruit;
            let fruitData = fruit.Data;
            let data = {
                id:fruitData.id,
                color:fruitData.color
            }
            let item = fruitBoomItem.getBoomItem(data);
            if(!this.game_group.contains(item))
            this.game_group.addChildAt(item,1);
            let w = this.fruit_group.localToGlobal(fruit.x, fruit.y,this.testPoint);
            let l = this.game_group.globalToLocal(w.x, w.y, this.testPoint);
            item.x = l.x;
            item.y = l.y;
            item.boom(fruit.boom, fruit);
            this.knifeHitNum++;
        }
        this.fruit_group.x = 360;
        this.fruit_group.y = 317;

        this.floatWorld();
        
        // let data  = {
        //     customs:GameManager.instance.curCustoms
        // }
        // AccountPrase.instance().account(data);
        this.nextSmallCustoms();
    }

    public playAgain()
    {
        this.game_group.touchEnabled = true;
        this.cut_fruitList = {};
        GameManager.instance.one_cut_num = 0;
        EffectUtil.instance().StopEffect(this.fruit_group);
        this.fruit_group.rotation = 0;
        this.fruit_list = [];
        let num = this.fruit_group.numChildren;
        for(let i=0;i<num;i++)
        {
            let fruit = this.fruit_group.getChildAt(i) as fruit;
            fruit.boom();
            fruit.reset();
        }
        GameManager.instance.init();
        this.isIncheck = false;
        this.curHitlost = 0;
        this.totalNum = 0;

        this.initFruitUI();
        this.initPosAndAnimation();
        this.initDragonTimes();
        this.updateCurCustoms();
        this.initStar();
    }

    /**
     * reset knife origin size
     */
    public resetBackKnifeScale()
    {
        if(this.knife.isScale)
        {
            this.knife.reBackScaleState();
        }
        if(this.knife1.isScale)
        {
            this.knife1.reBackScaleState();
        }
        if(this.knife2.isScale)
        {
            this.knife2.reBackScaleState();
        }
    }

    public nextCustoms(needCheck:boolean)
    {
        GameManager.instance.curCustoms++;
        GameManager.instance.curCustomsIndex = 1;
        this.cut_fruitList = {};
        GameManager.instance.one_cut_num = 0;
        this.touchChildren = false;
        this.curHitlost = 0;
        this.resetBackKnifeScale();
        if(needCheck)
        {
            GameManager.instance.checkIsHaveAward();
        }
        if(GameManager.instance.isCanshowBox)
        {
            GameManager.instance.isCanshowBox = false;
            this.playBoxAnimation();
        }
        this.initFruitUI();
        this.UpdateTittle(); 
        this.updateCurCustoms();
        this.initStar();
        this.randomSetTips();
        this.updateNextCustoms();
        this.touchChildren = true;
    }

    public nextSmallCustoms()
    {
        EffectUtil.instance().StopEffect(this.fruit_group);
        this.fruit_group.rotation = 0;
        // this.curHitlost = 0;
        let numchild = this.fruit_group.numChildren;
        for(let i=0;i<numchild;i++)
        {
            let fruit = this.fruit_group.getChildAt(i) as fruit;
            fruit.reset();
        }
        this.fruit_list = [];
        GameManager.instance.curCustomsIndex++;
        if(GameManager.instance.curCustomsIndex > 4)
        {
            let data  = {
                customs:GameManager.instance.curCustoms,
                fruit_list:JSON.stringify(this.cut_fruitList),
                one_cut_num:GameManager.instance.one_cut_num
            }
            AccountPrase.instance().account(data);
            return;
        }
        this.judgeWeaponIsHaveThree();
        this.initFruitUI();
        this.updateCurCustoms();
    }

    /**
     * 判断武器是否可以三发
     */
    public judgeWeaponIsHaveThree()
    {
        let threeTimes = GameManager.instance.curThreeTimes;
        if(threeTimes <= 0 && GameManager.instance.judgeIsHaveThree())
        {
            //没有三发的状态且可以获得三发
            GameManager.instance.curThreeTimes++;  
            MessageUtil.tips('三箭齐发效果触发！');
            this.updateThree();
            this.updateThreeGroup(true);
        }
    }

    private initPosAndAnimation()
    {
        EffectUtil.instance().StopEffect(this.knife);
        EffectUtil.instance().StopEffect(this.knife1);
        EffectUtil.instance().StopEffect(this.knife2);
        this.knife.y = GameConst.GameConst.knife_posY;
        if(this.system)
        this.system.emitterY = this.knife.y + 20;
        this.knife.rotation = 0;
        this.isIncheck = false;
        EffectUtil.instance().flyObj(this.knife, 300, 15);  
        this.updateThree();
        if(this.system)
        this.system.stop();
        this.particleInit();
        this.particleStop();
    }

    protected particleInit()
    {
        if(!this.system1)return;
        this.system1.emitterY = this.knife.y + 20;
        this.system2.emitterY = this.knife.y + 20;
    }

    protected particleMove()
    {
        if(!this.system1)return;
        if(!this.isKnifeInBack)
        {
            this.system1.emitterY -= GameConst.GameConst.knife_speed;
            this.system2.emitterY -= GameConst.GameConst.knife_speed;
        }
        else
        {
            this.system1.emitterY += GameConst.GameConst.knife_speed;
            this.system2.emitterY += GameConst.GameConst.knife_speed;
        }
    }

    public updateThree()
    {
        if(this.isIncheck)return;
        let three_times = GameManager.instance.curThreeTimes;

        if(three_times > 0)
        {
            this.knife1.visible = true;
            this.knife1.y = GameConst.GameConst.knife_posY + 40;
            this.knife1.rotation = 0;
            this.knife2.visible = true;
            this.knife2.y = GameConst.GameConst.knife_posY + 40;
            this.knife2.rotation = 0;
            EffectUtil.instance().StopEffect(this.knife);
            EffectUtil.instance().StopEffect(this.knife1);
            EffectUtil.instance().StopEffect(this.knife2);
            EffectUtil.instance().flyObj(this.knife, 300, 15);
            EffectUtil.instance().flyObj(this.knife1, 300, 15);  
            EffectUtil.instance().flyObj(this.knife2, 300, 15); 
        }
        else
        {
            this.knife1.visible = false;
            this.knife2.visible = false;
        }
    }

    public updateDragonGroup(isInit = false)
    {
        let dradonTimes = GameManager.instance.curDragonTimes;
        if(dradonTimes > 0)
        {
            if(!isInit)
            {
                // GameManager.instance.curDragonTimes--;
                GameManager.instance.reduceCurDragonTimes();
                dradonTimes -- ;
            }
            if(dradonTimes > 0)
            {
                this.dragon_group.visible = true;
                this.lb_dragon_times.text = 'x' + dradonTimes;
            }
            else{
                this.dragon_group.visible = false;
            }
        }
        else
        {
            this.dragon_group.visible = false;
        }
    }

    private gameOver()
    {
        this.game_group.touchEnabled = false;
        let isRelive = GameManager.instance.isRelive;
        if(!isRelive && !RoleCache.instance().reviewStatus)
        {
            let data = 
            {
                isPop:true,
                effect:0
            }
            UIManager.instance().openUILogic(ReliveUI, data);
        }
        else
        {
            // this.removeEvent();
            // UIManager.instance().openUILogic(MainUI);
            let data = {
                isPop:true,
                effect:0
            }
            UIManager.instance().openUILogic(GameOverUI, data);
        }
    }

    public relive()
    {
        GameManager.instance.isRelive = true;
        this.game_group.touchEnabled = true;
        this.curHitlost = 0;
        this.initStar();
    }

    protected initFruitUI(data?)
    {
        if(data)
        this.data = data;
        this.knifeHitNum = 0;
        let customsId = GameManager.instance.curCustoms;
        this.fruit_group.y = -this.fruit_group.height;
        this.fruit_group.x = GameConst.GameConst.fruit_group_x;
        if(customsId > GameConst.customsNums)
        {
            customsId = Math.ceil(Math.random() * GameConst.customsNums) + 1;
        } 
        this.fruit_list = [];
        let largeCustomsData = DataJson.largeCustoms_define[customsId];
        let arr = largeCustomsData.customs.split(',');
        let index = GameManager.instance.curCustomsIndex - 1;
        let customs = parseInt(arr[index]);
        let customsData = CustomsTpl.instance().getDataById(customs);
        if(customsData)
        {
            let shapeId = customsData.shape;
            let shape_data = DataJson.shape_data[shapeId];
            let fruit_str = customsData.fruit + '';
            let fruit_data = fruit_str.split(',');
            let index = 0;
            let money = 0;
            for(let k in shape_data)
            {   
                let pos_data = shape_data[k];
                let fruit_id = parseInt(fruit_data[index]);
                if(fruit_id > 22)
                fruit_id = 1;
                
                if(index >= fruit_data.length - 1)
                    index = 0;
                else
                    index++;
                
                let _fruit = this.getFruit(fruit_id);
                _fruit.randomRarotion();
                this.calculateFruitNum(fruit_id);
                if(GameManager.instance.curCustomsIsBoss)
                {
                    //是否是boss关卡
                    _fruit.setScale(0.7);
                }

                if(!this.fruit_group.contains(_fruit))
                {
                    this.fruit_group.addChild(_fruit);
                }
                this.fruit_list.push(_fruit);
                _fruit.x = pos_data.x;
                _fruit.y = pos_data.y;
            }
            this.totalNum = this.fruit_list.length;
            
            // this.curCustomsMoney = money;
            let rotation_id = customsData.roration;
            let rotation_data = DataJson.roration_data[rotation_id];
            // egret.setTimeout(()=>{
                
            // }, this, 1000);
            this.game_group.touchEnabled = true;
            egret.Tween.get(this.fruit_group).to({
                y:317
            }, 500).call(()=>{
                this.randomRotation(rotation_data);
            })
        }
    }

    /**
     * 计算水果个数
     */
    public calculateFruitNum(fruitId:number)
    {
        if(!this.cut_fruitList[fruitId])
        {
            this.cut_fruitList[fruitId] = 1;
        }
        else
        {
            this.cut_fruitList[fruitId]++;
        }
    }

    public showDragonEffect()
    {
        this.dragonEffect.visible = true;
        this.img_shadow.visible = true;
        this.dragonEffect.show();
        let self = this;
        let frame = 100/3;
        EffectUtil.instance().StopEffect(this.fruit_group);
        EffectUtil.instance().shakeScreen(this.fruit_group, 10);
        egret.Tween.get(this.img_shadow).to({
            alpha:1
        }, 30 * frame).call(()=>{
            self.rect_black.visible = true;
        }).wait(8 * frame).call(()=>{
            self.dragonEffect.showLight();
        })
        
        this.initPosAndAnimation();
        this.updateDragonGroup();
    }

    public getFruit(id)
    {
        let data = DataJson.fruit_data[id];
        let fruit = FruitPool.getFruit(data);
        return fruit;
    }

    public randomRotation(rotation_data)
    {
        if(rotation_data)
        {
            let str = rotation_data.json;
            let time = rotation_data.time;
            let springback = rotation_data.springback;
            EffectUtil.instance().StopEffect(this.fruit_group);
            let isJump = false;
            if(GameManager.instance.curCustomsIsBoss || Math.random() > 0.7)
            {
                isJump = true;
            }
            EffectUtil.instance().excuteAnimation(str,this.fruit_group,time,springback,isJump);
        }
    }

    public removeEvent()
    {
        this.game_group.removeEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touchHandle, this);
        egret.stopTick(this.update, this);

        this.btn_change.removeTapHandler();
        this.btn_share.removeTapHandler();
        this.btn_three.removeTapHandler();
        GameUtil.removeEventListener(EventConst.SHARE_SUCCESS, this.shareSuccess, this);
        GameUtil.removeEventListener(EventConst.CHANGE_WEAPON, this.changeWeapon, this);
    }

    public clear()
    {
        super.clear();
        /**
         * 清除所有动画
         */
        EffectUtil.instance().StopEffect(this.btn_share);
        EffectUtil.instance().StopEffect(this.fruit_group);
        EffectUtil.instance().StopEffect(this.group_tips);
        
        /**
         * 清除所有类变量
         */
        this.isIncheck = null;
        this.count = null;
        this.testPoint = null;
        this.isPlayBoxAnimation = null;
        this.speedX =null ;
        this.speedY =null ;
        this.isBackX = null;
        this.isBackY = null;
        this.isKnifeInBack = null;
        this.BackStayTime = null;
        this.totalNum = null;
        GameManager.instance.one_cut_num = 0;

        if(this.system)
        {
            this.system.stop();
            this.system.parent.removeChild(this.system);
            this.system = null;
        }
        if(this.system1)
        {
            this.system1.stop();
            this.system1.parent.removeChild(this.system1);            
            this.system1 = null;
        }
        if(this.system2)
        {
            this.system2.stop();
            this.system2.parent.removeChild(this.system2);                        
            this.system2 = null;
        }
        
        let length = this.fruit_list.length;
        for(let i=0;i<length;i++)
        {
            let fruit = this.fruit_list[i] as fruit;
            this.fruit_list.splice(i,1);
            fruit.remove();
            fruit = null;
            i--;
            length--;
        }
        this.fruit_list = [];
        
        this.knife.remove();
        this.knife1.remove();
        this.knife2.remove();
        this.knife = null;
        this.knife1 = null;
        this.knife2 = null;
        this.dragonEffect.remove();
        this.dragonEffect = null;

        if(this.gif_box && this.gif_box.parent)
        {
            this.gif_box.stop();
            this.gif_box.parent.removeChild(this.gif_box);
            this.gif_box = null;
        }

        if(this.leaf)
        {
            this.leaf.stop();
            this.leaf.parent.removeChild(this.leaf);
            this.leaf = null;
        }

        FruitPool.clearPool();
        fruitBoomItem.clearPool();
    }
}